# -*-coding:utf-8-*-
import sys
import requests
from sqlalchemy import and_
from props import properties
from DAO.book import book
from DAO.tag import tag
from DAO.book_tag import book_tag_ref
from props.sql_connect import DBSession
from threads import book_task_fail_queue, save_queue
from crawler_base import crawler_base
import datetime
import json
import time

reload(sys)
sys.setdefaultencoding('utf-8')
props = properties.props
get_header = properties.get_header


class update_api_crawler(crawler_base):
    session = None

    def __init__(self):
        self.session = DBSession()
        crawler_base.__init__(self)

    def get_json_by_bookid(self, bookID, proxy):
        url = props.get('book_api.url') + bookID
        try:
            book_page = requests.get(url, headers=get_header('api'), proxies=proxy, timeout=5)
            book_json = json.loads(book_page.content.decode("utf-8", 'ignore'))
            if book_json.get('code') == 112:
                print proxy['http'] + 'Forbidden'
                self.success = False
                return
            self.get_info_by_json(book_json, bookID)
        except requests.RequestException, req:
            print 'update_api.request:', req
            self.success = False
            return
        except Exception, e:
            print 'update_api.exception:', e
        return

    def get_info_by_json(self, book_json, bookID):
        newbook = self.session.query(book).get(bookID)

        name = book_json.get('title')

        rating = book_json.get('rating')
        tag_info = book_json.get('tags')
        self.deal_taginfo(tag_info, bookID, newbook.tag)

        newbook.author = ','.join(book_json.get('author'))
        newbook.publisher = book_json.get('publisher')
        newbook.pre_name = book_json.get('origin_title')
        newbook.translator = ','.join(book_json.get('translator'))
        newbook.date = book_json.get('pubdate')
        newbook.pages = book_json.get('pages')
        newbook.price = book_json.get('price')
        newbook.ISBN = book_json.get('isbn13')
        newbook.star = rating.get('average')
        newbook.image_url = book_json.get('images').get('large')
        newbook.content_des = book_json.get('summary')
        newbook.author_des = book_json.get('author_intro')
        newbook.last_update = datetime.datetime.now()

        self.bundle.append(newbook)
        time.sleep(5)

        print 'update_api_end'
        return True

    def deal_taginfo(self, taginfo, bookID, father_tag):
        query = self.session.query(tag)
        father_tag_id = query.filter(and_(tag.tag_name == father_tag),(tag.tag_level == 1)).all()[0].tag_id
        for i in range(len(taginfo)):
            newtag = None
            info = taginfo[i]
            tag_title = info.get('title')
            tag_ref = query.filter(and_((tag.father_tag_id == father_tag_id), (tag.tag_level == 2), (tag.tag_name == tag_title))).all()
            if len(tag_ref) == 0:
                newtag = tag(tag_title, 2, father_tag_id)
                self.bundle.append(newtag)
            count = info.get('count')
            ref = book_tag_ref(bookID, tag_title, count)
            self.bundle.append(ref)
        print

    def work(self, param=None, proxy=None):
        self.get_json_by_bookid(param, proxy)
        try:
            if len(self.bundle) > 0:
                save_queue.put(self.bundle)
            if not self.success:
                book_task_fail_queue.put_nowait(param)
        except Exception, e:
            print 'queue put_error:', e
        self.session.close()
        return self.success
